//查询用户的收藏
function getColl(userId) {
    $.get(
        "http://localhost:8080/queryCollects",
        { "userId": userId },
        function (result) {
            if (result.code == 1) {
                displayColl(result.games)
            }
        }
    )
}

//显示收藏的游戏
function displayColl(game) {
    initColl()//初始化第一行
    for (var i = 0; i < game.length; i++) {
        price = game[i].gamePrice + " 元"
        $("div#info:last div div strong").html(game[i].gameName)
        $("div#info:last div div p").html(game[i].gameId)
        $("div#info:last div span").html(price)

        addCollRow()
    }
    $("div#info:last").remove()//移除多出来一行
}

//添加一条信息栏
function addCollRow() {
    $("div#info:last").after('<div class="media text-muted pt-3" id="info">' +
        '<div class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">' +
        '<div class="d-flex justify-content-between align-items-center w-100">' +
        '<strong class="text-gray-dark">游戏1</strong>' +
        '<p style="display:none">gameid</p>' +
        '<a href="#" id="dele">删除</a>' +
        '</div>' +
        '<span class="d-block">价格</span>' +
        '</div>' +
        '</div>')
    rebound()
}

//一条信息都没有的时候，生成第一条
function initColl() {
    $("h6#hcoll").after('<div class="media text-muted pt-3" id="info">' +
        '<div class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">' +
        '<div class="d-flex justify-content-between align-items-center w-100">' +
        '<strong class="text-gray-dark">游戏1</strong>' +
        '<p style="display:none">gameid</p>' +
        '<a href="#" id="dele">删除</a>' +
        '</div>' +
        '<span class="d-block">价格</span>' +
        '</div>' +
        '</div>')
    rebound()
}

//绑定按键事件
function rebound() {
    $("a#dele:last").on("click", function () {
        event.preventDefault()
        gameId = $(this).siblings("p").html()
        rmColl($.cookie('user_id'), gameId)
    })
}

//删除收藏
function rmColl(userId, gameId) {
    $.get(
        "http://localhost:8080/removeCollect",
        { "userId": userId, "gameId": gameId },
        function (result) {
            if (result.code == 1) {
                alert("删除收藏成功")
                location.reload()
            } else {
                alert("删除收藏失败")
            }
        }
    )
}

//-----------------------------------------------------已购部分
//查询已购游戏
function getPurchased(userID) {
    $.get(
        "http://localhost:8080/queryOrders",
        { "userID": userID },
        function (result) {
            displayPur(result)
        }
    )
}

function displayPur(result) {
    initPur()
    for (var i = 0; i < result.length; i++) {
        $("div#pinfo:last div div strong").html(result[i].gameName)
        $("div#pinfo:last div span").html(result[i].orderDate.slice(0, 10))
        $("div#pinfo:last div p").html(result[i].orderNum)

        addPurRow()
    }
    $("div#pinfo:last").remove()
}

//添加一条信息栏
function addPurRow() {
    $("div#pinfo:last").after('<div class="media text-muted pt-3" id="pinfo">' +
        '<div class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">' +
        '<div class="d-flex justify-content-between align-items-center w-100">' +
        '<strong class="text-gray-dark">游戏1</strong>' +
        '<p style="display:none">id</p>' +
        '<a href="#" id="refund">退款</a>' +
        '</div>' +
        '<span class="d-block">价格</span>' +
        '</div>' +
        '</div>')
    rebind()
}

//一条信息都没有的时候，生成第一条
function initPur() {
    $("h6#hpur").after('<div class="media text-muted pt-3" id="pinfo">' +
        '<div class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">' +
        '<div class="d-flex justify-content-between align-items-center w-100">' +
        '<strong class="text-gray-dark">游戏1</strong>' +
        '<p style="display:none">id</p>' +
        '<a href="#" id="refund">退款</a>' +
        '</div>' +
        '<span class="d-block">价格</span>' +
        '</div>' +
        '</div>')
    rebind()
}

//删除订单功能
function rebind() {
    $("a#refund:last").on("click", function () {
        event.preventDefault()
        order_id = $(this).siblings("p").html()
        console.log(order_id)
        console.log(typeof order_id)
        $.get(
            "http://localhost:8080/removeOrder",
            { "orderID": order_id },
            function (result) {
                if (result.code == 1) {
                    alert("删除订单成功")
                    location.reload()
                } else {
                    alert("删除订单失败")
                }
            }
        )
    })
}